
#include <stdint.h>
#include <string.h>
#include <stdio.h>


#include "userButton.h"


#define GET_K1_STA()    nrf_gpio_pin_read(_K1_PIN_)
#define GET_K2_STA()    nrf_gpio_pin_read(_K2_PIN_)
#define GET_K3_STA()    nrf_gpio_pin_read(_K3_PIN_)

static uint16_t K1_count = 0;             
static uint8_t  K1_shortPressDown_f = 0;  
static uint8_t  K1_shortPressUp_f = 0;    
static uint8_t  K1_longPress_f = 0;       
static uint8_t  K1_hold_f = 0;           

static uint16_t K2_count = 0;             
static uint8_t  K2_shortPressDown_f = 0;   
static uint8_t  K2_shortPressUp_f = 0;     
static uint8_t  K2_longPress_f = 0;        
static uint8_t  K2_hold_f = 0;             

static uint16_t K3_count = 0;              
static uint8_t  K3_shortPressDown_f = 0;   
static uint8_t  K3_shortPressUp_f = 0;     
static uint8_t  K3_longPress_f = 0;        
static uint8_t  K3_hold_f = 0;             


void clear_all_key_event(void)
{
	K1_shortPressDown_f = 0;          
	K1_shortPressUp_f = 0;            
	K1_longPress_f = 0;          
	K1_hold_f = 0;  

	K2_shortPressDown_f = 0;          
	K2_shortPressUp_f = 0;            
	K2_longPress_f = 0;          
	K2_hold_f = 0;
	
	K3_shortPressDown_f = 0;          
	K3_shortPressUp_f = 0;            
	K3_longPress_f = 0;          
	K3_hold_f = 0;
}


uint8_t getK1_ShortPressDown(void)
{
	uint8_t temp = K1_shortPressDown_f;
	K1_shortPressDown_f = 0;            
	return temp;
}


uint8_t getK1_ShortPressUp(void)
{
	uint8_t temp = K1_shortPressUp_f;
	K1_shortPressUp_f = 0;             
	return temp;
}


uint8_t getK1_LongPress(void)
{
	return K1_longPress_f;             
}

 
uint8_t getK1_PressHold(void)
{
	return K1_hold_f;                  
}

 
 
uint8_t getK2_ShortPressDown(void)
{
	uint8_t temp = K2_shortPressDown_f;
	K2_shortPressDown_f = 0;              
	return temp;
}

 
uint8_t getK2_ShortPressUp(void)
{
	uint8_t temp = K2_shortPressUp_f;
	K2_shortPressUp_f = 0;              
	return temp;
}

 
uint8_t getK2_LongPress(void)
{
	return K2_longPress_f;             
}

 
uint8_t getK2_PressHold(void)
{
	return K2_hold_f;                  
}


 
 
uint8_t getK3_ShortPressDown(void)
{
	uint8_t temp = K3_shortPressDown_f;
	K3_shortPressDown_f = 0;              
	return temp;
}

 
uint8_t getK3_ShortPressUp(void)
{
	uint8_t temp = K3_shortPressUp_f;
	K3_shortPressUp_f = 0;              
	return temp;
}

 
uint8_t getK3_LongPress(void)
{
	return K3_longPress_f;             
}

 
uint8_t getK3_PressHold(void)
{
	return K3_hold_f;                  
}

 
void buttonDet(void)
{
	 
	//if(nrf_gpio_pin_read(K1_PIN) == 0)  
    if(0 == GET_K1_STA())
	{
		K1_count++;
		if(K1_count == 3)                   
		{
			K1_shortPressDown_f = 1;        
		}
		if(K1_count >= 100)                 
		{
			if(K1_hold_f == 0)
			{
				K1_hold_f = 1;              
				K1_longPress_f = 1;         
			}				
		}

	}
	else     
	{
		if(K1_count > 2 && K1_count < 100)     
		{   
			K1_shortPressUp_f = 1;             
		}
		K1_longPress_f = 0;                    
		K1_count = 0;                          
		K1_hold_f = 0;                         
	}
	
	 
	//if(nrf_gpio_pin_read(K2_PIN) == 0)
    if(0 == GET_K2_STA())    
	{
		K2_count++;
		if(K2_count == 3)                   
		{
			K2_shortPressDown_f = 1;        
		}
		if(K2_count >= 100)                 
		{
			if(K2_hold_f == 0)
			{
				K2_hold_f = 1;              
				K2_longPress_f = 1;         
			}				
		}

	}
	else     
	{
		if(K2_count > 2 && K2_count < 100)     
		{   
			K2_shortPressUp_f = 1;             
		}
		K2_longPress_f = 0;                    
		K2_count = 0;                          
		K2_hold_f = 0;                         
	}
	
	 
	//if(nrf_gpio_pin_read(K3_PIN) == 0)   
    if(0 == GET_K3_STA())    
	{
		K3_count++;
		if(K3_count == 3)                   
		{
			K3_shortPressDown_f = 1;        
		}
		if(K3_count >= 100)                 
		{
			if(K3_hold_f == 0)
			{
				K3_hold_f = 1;              
				K3_longPress_f = 1;         
			}				
		}

	}
	else     
	{
		if(K3_count > 2 && K3_count < 100)     
		{   
			K3_shortPressUp_f = 1;             
		}
		K3_longPress_f = 0;                    
		K3_count = 0;                          
		K3_hold_f = 0;                         
	}
}






























